# 3.31 Joystick Control Servo ## 3.31.1 Overview We control the servo via the axis X of the joystick. This model is widely applied to mechanical ON/OFF of lights and doors. ## 3.31.2 Test Code ### 3.31.2.1 Code Flow ![6-31-3-1](./media/6-31-3-1.png) ### 3.31.2.2 Build Code There are two ways to upload the code: directly open the code file we provide; or manually build blocks. **Directly open the code file we provide:** 1. Click ![](./media/j68.png) and choose `Load from your computer` ![](./media/j67.png) 2. We have already downloaded the codes on computer desktop, so open the file and choose `3-31-remoteControlServo.sb3` **Manually build blocks:** 1. Build the two basic blocks:![6-1-4-1-1](./media/6-1-4-1-1.png) 2. Declare a variable *angle* and assign the x value of joystick to *angle*. ![QQ_1722042398784](./media/6-31-3-2-1.png) 3. Add a ![j25](./media/j25.png) to determine whether *angle* > 3500. If yes, servo rotates to 0 degree. ![QQ_1722042751659](./media/6-31-3-2-2.png) 4. Add a ![j25](./media/j25.png) to determine whether *angle* < 500. If yes, servo rotates to 180 degree. ![image-20240727091322352](./media/6-31-3-2-3.png) **Complete Test Code** ![QQ_1722040933018](./media/6-31-3-2-4.png) ### 3.31.2.3 Test Result After uploading code, push the joystick to the left and the servo rotates to 180 degree. Push it to the right and the servo rotates to 0 degree.